import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/*增加按钮事件
* */
public class MyPanel extends JPanel {
    public JLabel jLabel = new JLabel("[00:00:00]");//记录当前时间
    JButton jButton = new JButton("Start");  //Status  ::: start starting restart
    public MyPanel() {
        add(jButton);
        add(jLabel);

       //注册事件
        jButton.addActionListener(new MyActionListener_jButton());

    }

    //return formatted time for jlabel
    private String showTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("[HH:mm:ss]");
        return sdf.format(new Date());
    }

    //定义一个内部类
    /*我的监听器--和jButton对象绑定的
     * */
    class MyActionListener_jButton implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //do something
            System.out.println("按钮被点击");
            //内部类访问外部类的成员
            //显示时间
            jLabel.setText(showTime());
            //改变状态
            jButton.setText("starting");


        }
    }

}

class Test{

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("测试第三级组件");
        jFrame.setLocation(300,400);
        jFrame.setSize(400,400);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(new FlowLayout());

        MyPanel myPanel = new MyPanel();
        jFrame.add(myPanel);
        jFrame.setVisible(true);
    }

}
